import pymysql
import yaml

from frame.utils.log_util import logger


class Utils:

    @classmethod
    def get_yaml_data(cls, file_path):
        """
        读取 yaml 文件
        :param file_path:
        :return:
        """
        with open(file_path, encoding="utf-8") as f:
            datas = yaml.safe_load(f)
        return datas

    @classmethod
    def query_db(cls, sql, database_info):
        """
        查询数据库
        :param sql: 要执行的 SQL 语句
        :param database_info: 数据库的连接信息
        :return:
        """
        # 连接数据库
        conn = pymysql.Connect(**database_info)
        # 创建游标
        cursor = conn.cursor()
        # 执行 sql 语句
        cursor.execute(sql)
        # 获取查询结果
        datas = cursor.fetchall()
        logger.info(f"数据库查询结果为 {datas}")
        # 关闭连接
        cursor.close()
        conn.close()
        return datas




